#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX_N 60005

struct node
{
    int val;
    int cnt,bigger;
    int lch,rch;
} tree[MAX_N];
int cnt_tree;
int a[MAX_N];
int lx[MAX_N];
int root ;

int mktree(int l,int r)
{
    if (l>r) return -1;
    int m = (l+r) / 2;
    int lch = mktree(l,m-1);
    int rch = mktree(m+1,r);
    cnt_tree++;
    tree[cnt_tree].lch = lch;
    tree[cnt_tree].rch = rch;
    tree[cnt_tree].val = lx[m];
    tree[cnt_tree].cnt = 0;
    tree[cnt_tree].bigger = 0;
    return cnt_tree;
}

void Insert(int x,int root)
{
    if (tree[root].val > x) Insert(x,tree[root].lch);
    if (tree[root].val < x)
    {
        tree[root].bigger ++;
        Insert(x,tree[root].rch);
    }
    if (tree[root].val == x) tree[root].cnt++;
}

int get_bigger(int x, int root)
{
    if (x==tree[root].val)
        return tree[root].bigger+tree[root].cnt;
    if (x>tree[root].val)
        return get_bigger(x,tree[root].rch);
    return tree[root].cnt+tree[root].bigger+get_bigger(x,tree[root].lch);
}


int main()
{
  //  freopen("1794.txt","r",stdin);
    int T;
    cin >> T;
    for (int ncase = 1 ; ncase <= T ; ncase ++)
    {
        int m,n,x,y;
        scanf("%d%d",&m,&n);
        for (int i = 0 ; i <  m +n ; i++)
        {
            scanf("%d%d",&x,&y);
            a[x-1] = y;
            lx[x-1] = y;
        }
        int j = 0;

        sort(lx,lx+m+n);



        for (int i = 1 ; i<m+n; i++)
        {
            if (lx[i] != lx[j]) lx[++j] = lx[i];
        }



        memset(tree,0,sizeof(tree));
        cnt_tree =0;
        root =  mktree(0,j);

        int ans = 0;
        for(int  i= 0 ; i< m+n ; i++)
        {
            ans += get_bigger(a[i],root);
            Insert(a[i],root);
        }

        printf("Scenario #%d:\n%d\n\n",ncase,ans);
    }


    return 0;
}
